package com.syn.synold4s.control;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import com.gaoyi.basemodule.baseControl.BaseView;
import com.syn.synold4s.R;
import com.syn.synold4s.databinding.ToolbarTilteViewBinding;

public class ToolBarTitleView extends BaseView<ToolbarTilteViewBinding> {

    public ToolBarTitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void initView(TypedArray ta) {
        //toolbar居中文本
        String title = ta.getString(R.styleable.ToolBarTitleView_toolbar_title_text);
        //toolbar左边的操作图标
        int leftSrc = ta.getResourceId(R.styleable.ToolBarTitleView_toolbar_left_src, R.drawable.ic_menu);
        //toolbar背景颜色
        int background = ta.getResourceId(R.styleable.ToolBarTitleView_toolbar_background, R.drawable.toolbar_title_bg);
        //设置右边logo的可见性
        int visibility = ta.getInt(R.styleable.ToolBarTitleView_toolbar_logo_visible, View.VISIBLE);
        setLogoVisibility(visibility);

        mBinding.rlTitle.setNavigationIcon(leftSrc);
        mBinding.rlTitle.setBackgroundResource(background);
        if (!TextUtils.isEmpty(title)) {
            mBinding.titleTv.setText(title);
        }
    }

    public void setOnBackListener(OnBackListener onBackListener) {
        mBinding.rlTitle.setNavigationOnClickListener(view -> onBackListener.onBackListener());
    }

    /**
     * 设置toolbar title 文本
     */
    public void setTitleText(String text) {
        mBinding.titleTv.setText(text);
    }

    public void setLogoVisibility(int visibility) {
        switch (visibility) {
            case 0: // visible
                mBinding.ivLogo.setVisibility(View.VISIBLE);
                break;
            case 1: // invisible
                mBinding.ivLogo.setVisibility(View.INVISIBLE);
                break;
            case 2: // gone
                mBinding.ivLogo.setVisibility(View.GONE);
                break;
        }
    }

    /**
     * 设置toolbar左边操作图标可见性
     */
//    public void setOperationIconVisibility(int visibility) {
//        mBinding.rlTitle.setNavigationIcon();
//    }

    public interface OnBackListener {
        void onBackListener();
    }

    @Override
    protected ToolbarTilteViewBinding getViewBinding() {
        return ToolbarTilteViewBinding.inflate(LayoutInflater.from(mContext));
    }

    @Override
    protected int[] getAttrs() {
        return R.styleable.ToolBarTitleView;
    }
}
